//
//  MyCollectionController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/31.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "MyCollectionController.h"
#import "MyCollectionViewCell.h"
#import "CollectionNullView.h"

@interface MyCollectionController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic , strong) NSMutableArray * array;
@property (nonatomic , strong) CollectionNullView * nullView;

@property (nonatomic , assign) NSInteger index;

@end

@implementation MyCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)setNavi
{
    self.title = @"我的收藏";
}

-(void)setUI
{
    self.view.backgroundColor =  Color(245, 245, 245);
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.rowHeight = 115;
    [self.tableview registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableview.tableFooterView = [[UIView alloc]init];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableview.mj_header beginRefreshing];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
}

- (void)requestData
{
    self.index = 1;
    [self requestMoreData];
}

-(void)requestMoreData
{
    NSDictionary * dict = @{@"token":self.userToken,@"page":[NSString stringWithFormat:@"%ld",self.index]};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@user-collection/index",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        if (isSuccess) {
            if (self.index == 1) {
                self.array = [NSMutableArray array];
            }
            if (self.index == 1 && [[responseObject objectForKey:@"data"] count] == 0) {  //如果是第一页 还没有数据的话  显示空视图
                [self.view addSubview:self.nullView];
            }else{
                [self.nullView removeFromSuperview];
                for (int i = 0; i < [[responseObject objectForKey:@"data"] count]; i ++) {
                    CollectionGoodsModel * model = [[CollectionGoodsModel alloc]init];
                    [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"][i]];
                    [self.array addObject:model];
                }
            }
            if ([responseObject[@"hasmore"] integerValue] == 0) {
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            }
            self.index += 1;
            [self.tableview reloadData];
        }
        [self endRefresh];
    } failure:^(NSError *error) {
        [self endRefresh];
    }];
}

- (void)endRefresh
{
    if (self.tableview.mj_header.isRefreshing) {
        [self.tableview.mj_header endRefreshing];
    }else
    {
        [self.tableview.mj_footer endRefreshing];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setCellWithModel:self.array[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 从数据源中删除
    [self.array removeObjectAtIndex: indexPath.row];
    // 从列表中删除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

-(CollectionNullView *)nullView
{
    if (!_nullView) {
        _nullView = [[[NSBundle mainBundle] loadNibNamed:@"CollectionNullView" owner:self options:nil] lastObject];
        _nullView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }
    return _nullView;
}
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
